package com.youyi.yyfilepickerlibrary.core.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

public class FragmentPagerAdapter extends FragmentStatePagerAdapter {
    private FragmentManager fm;
    private List<Fragment> fragmentList;
    private List<String> titleArray;

    @Override // android.support.v4.view.PagerAdapter
    public int getItemPosition(Object obj) {
        return -2;
    }

    public FragmentPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        this.fm = fragmentManager;
    }

    public FragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> list, List<String> list2) {
        this(fragmentManager);
        this.fragmentList = list;
        this.titleArray = list2;
    }

    public FragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> list) {
        this(fragmentManager);
        this.fragmentList = list;
    }

    public void setFragments(ArrayList<Fragment> arrayList) {
        if (this.fragmentList != null) {
            FragmentTransaction beginTransaction = this.fm.beginTransaction();
            for (Fragment fragment : this.fragmentList) {
                beginTransaction.remove(fragment);
            }
            beginTransaction.commit();
            this.fm.executePendingTransactions();
        }
        this.fragmentList = arrayList;
        notifyDataSetChanged();
    }

    @Override // android.support.v4.view.PagerAdapter
    public CharSequence getPageTitle(int i) {
        List<String> list = this.titleArray;
        return list != null ? list.get(i) : "";
    }

    @Override // android.support.v4.view.PagerAdapter
    public int getCount() {
        return this.fragmentList.size();
    }

    @Override // android.support.v4.app.FragmentStatePagerAdapter
    public Fragment getItem(int i) {
        return this.fragmentList.get(i);
    }

    @Override // android.support.v4.app.FragmentStatePagerAdapter, android.support.v4.view.PagerAdapter
    public Object instantiateItem(ViewGroup viewGroup, int i) {
        return super.instantiateItem(viewGroup, i);
    }
}
